package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.StudentBean;
import com.project.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService service;

    @RequestMapping("/findAll")
    @ResponseBody
    public String findAll(
            //如果客户端提交的pageNO为null，那么将默认值1赋给pageNO形参
            @RequestParam(value = "pageNO",defaultValue = "1") int pageNO){
        //调用业务方法，得到分页对象
        PageInfo<StudentBean> pageInfo = service.findAll(pageNO);
        String info = "<table border='1' cellspacing='0' width='50%'>";
        info += "<thead><tr><th>姓名</th><th>生日</th><th>成绩</th><th>操作</th></tr></thead>";
        info += "<tbody>";

        for(StudentBean student : pageInfo.getList()){
            info += "<tr><td>"+student.getName()+"</td>" +
                    "<td>"+student.getBirthday()+"</td>" +
                    "<td>"+student.getGrade()+"</td>" +
//                    "<td><a href='/student/del?id="+student.getId()+"'>删除</a> " +
                    "<td><a href='/student/del/"+student.getId()+"'>删除</a> "+
                    " <a href='/student/findById?id="+student.getId()+"'>修改</a></td></tr>";
        }

        info += "</tbody></table>";

        for(int i=1;i<=pageInfo.getPages();i++){
            info += "<a style='margin-left:10px;' href='/student/findAll?pageNO="+i+"'>"+i+"</a>";
        }
        info+= "<div><a href='/html/add.html'>添加</a></div>";

        return info;
    }

//    @RequestMapping("/del")
//    public String del(int id){
//        service.del(id);
//        return "redirect:/student/findAll";
//    }
    @RequestMapping("/del/{ss}") //rest风格
    public String del(@PathVariable("ss") int studentid){
        service.del(studentid);
        return "redirect:/student/findAll";
    }

    @RequestMapping(value="/add",method = RequestMethod.POST)
    public String add(StudentBean student){
        service.add(student);
        return "redirect:/student/findAll";
    }

    @RequestMapping("/findById")
    @ResponseBody
    public String findById(int id){
       StudentBean student =  service.findById(id);
       String info = "<form action='/student/update'>";

       info += "姓名："+student.getName()+"<br>";
       info += "生日："+student.getBirthday()+"<br>";
       info += "成绩：<input type='text' name='grade' value='"+student.getGrade()+"'><br>";
       info += "<input type='hidden' name='id' value='"+id+"'>";
       info += "<input type='submit' value='修改'>";

        info += "</form>";
       return info;
    }

    @RequestMapping("/update")
    public String update(int id,int grade){
        service.update(id, grade);
        return "redirect:/student/findAll";
    }

}
